package _003;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

/**
 * Solution
 *
 * @author yuand
 * @date 2021/8/24
 **/
public class Solution {

    public static void main(String[] args) {
        int num = new Solution().lengthOfLongestSubstring("tmmzuxt");
        System.out.println(num);
    }

    public int lengthOfLongestSubstring(String s) {
        int len = s.length();
        int win_left = 0;
        int win_right = 0;
        int max = 0;
        Map<Character, Integer> map = new HashMap<>();
        while (win_right < len) {
            char cur = s.charAt(win_right);
            if (!map.containsKey(cur)) {
                // 未重复的，
                int size = win_right - win_left + 1;
                if (size > max) max = size;
            } else {
                // 重现重复
                int newLeft = map.get(cur) + 1;
                while (win_left < newLeft) {
                    map.remove(s.charAt(win_left));
                    win_left++;
                }
            }
            map.put(cur, win_right);
            win_right++;

        }
        return max;
    }
}
